
$(document).ready(function(){
	// Validate patient's birthday
    $('input#DOB_yyyy').blur(function() {
        checkBirthday($(this),$('#DOB_mm'),$('#DOB_dd'));
    });
    $('input#DOB_mm').blur(function(){
        checkMonth($(this),false,'','statusMessage');
    });   
    $('input#DOB_dd').blur(function(){
        checkDay($(this),false,'','statusMessage');
    });
    $('input#personAgeTxt').change(function() {       
        checkAge($(this));
    });
    
    // Validate patient's check-in date, time
    $('input#checkIn_mm').blur(function(){
        checkMonth($(this),false,'','statusMessage');
    });   
    $('input#checkIn_dd').blur(function(){
        checkDay($(this),false,'','statusMessage');
    });
    
    // Validate patient's death time
    $('input#death_mm').blur(function(){
        checkMonth($(this),false,'','statusMessage');
    });   
    $('input#death_dd').blur(function(){
        checkDay($(this),false,'','statusMessage');
    });
    
    // Validate patient's check-out death time
    $('input#checkOutDeath_mm').blur(function(){
        checkMonth($(this),false,'','statusMessage');
    });   
    $('input#checkOutDeath_dd').blur(function(){
        checkDay($(this),false,'','statusMessage');
    });
    // Province changed
    $('select#provinceLbx').change(function() {
        provinceChange('provinceLbx', 'districtLbx');       
    });
    // Set autocomplete for ward input
    setAutoComplete2("wardNameTxt", "../Patient/getWardFromDistrictAjax?set_ajax_view&part=","district_id","districtLbx");
    
    addDatePickerTime("#death_dd",'#death_mm','#death_yyyy','#deathTimeTxt',false,false,false);
    addDatePickerTime("#checkIn_dd",'#checkIn_mm','#checkIn_yyyy','#checkInTimeTxt',false,false,false);
    addDatePickerTime("#checkOutDeath_dd",'#checkOutDeath_mm','#checkOutDeath_yyyy','#checkOutDeathTimeTxt',false,false,true);
     
    $('#presideTxt').blur(function(){isNotEmpty($('#presideTxt'), _('Preside cannot be left empty', 'statusMessage'));});
    $('#secretaryTxt').blur(function(){isNotEmpty($('#secretaryTxt'), _('Secretary cannot be left empty'), 'statusMessage');});
    //$('#medicaTxt').blur(function(){isNotEmpty($('#medicaTxt'), _('Summary of treatment process cannot be left empty', 'statusMessage'));});
	//$('#conclusionTxt').blur(function(){isNotEmpty($('#conclusionTxt'), _('Conclusion cannot be left empty', 'statusMessage'));});
   
})

function provinceChange(provinceId, districtId) {
    $.ajax ({
        type:    "GET",
        url:      "../Patient/getDistrictFromProvinceAjax?set_ajax_view",
        data:     "provice_id=" + $('#' + provinceId).val(),
        dataType: "json",
        success:    function(data) {
            if(data.districtList.length > 0){
                $('#' + districtId).html(data.districtList);
            }else{
                var option  =   "<option value='0' label='- " + _('District') + " -'>- "  + _('District') + " -</option>";
                $('#' + districtId).html(option);
            }
        }
    });
}


function getDeadInsideInfo() {
    
    var submitData = $('#emerDeathFrm').formSerialize();
    return submitData;
}

function isReadyToSubmitDeath() {		
	if (!checkBirthday($('input#DOB_yyyy'),$('input#DOB_mm'), $('input#DOB_dd'))) {
        return false;
    }     
    if(!checkTimeDeath()){
        return false;
    }
    if(!checkTimeCheckOutDeath){
    	return false;
    }
    if(!validateSelectBox('doctorLbx',_("Please select doctor"),'statusMessage')){
        return false;
    }
    if(!isNotEmpty($('#presideTxt'), _('Preside cannot be left empty'), 'statusMessage')){
    	return false;
    }
    if(!isNotEmpty($('#secretaryTxt'), _('Secretary cannot be left empty'), 'statusMessage')){
    	return false;
    }
    /*if(!isNotEmpty($('#medicaTxt'), _('Summary of treatment process cannot be left empty'), 'statusMessage')){
    	return false;
    }
    if(!isNotEmpty($('#conclusionTxt'), _('Conclusion cannot be left empty'), 'statusMessage')){
    	return false;
    }*/
    $('span#statusMessage').text('');
    return true;
}

function checkTimeDeath(){
    var res =   checkFullDate($('#death_yyyy'), $('#death_mm'), $('#death_dd'), true,  _('Empty death date'), 'statusMessage', true);
    if(!res){
        return false;
    }
    res = checkHour($('#deathTimeTxt'), true, _('Invalid time'), 'statusMessage');
    if(!res){
        return false;
    }
    return true;
}

function checkTimeCheckOutDeath(){
    var res =   checkFullDate($('#checkOutDeath_yyyy'), $('#checkOutDeath_mm'), $('#checkOutDeath_dd'), true,  _('Empty check-out death date'), 'statusMessage', true);
    if(!res){
        return false;
    }
    res = checkHour($('#checkOutDeathTimeTxt'), true, _('Invalid time'), 'statusMessage');
    if(!res){
        return false;
    }
    return true;
}

function checkBirthday(objYear, objMonth, objDay) {
    var res     =   checkDate(objYear, objMonth, objDay, false, _('Empty date of birth'), 'statusMessage', true);
    if(!res){
        return false;
    }
    $('input#personAgeTxt').css("border-color", "#AAA");
    getAge('DOB_yyyy','personAgeTxt');
    return true;    
}

function checkAge(obj) {
	if (obj.val().length > 0 && !obj.val().match(/^[0-9]+$/)) {
	    obj.css("border-color", "red");
	    obj.focus();
	    $('span#statusMessage').text(_("Invalid age")).show();
	} else if (obj.val().length > 0) {
	    obj.css("border-color", "#AAA");
	    $('input#DOB_yyyy').css("border-color", "#AAA");
	    $('input#DOB_mm').css("border-color", "#AAA");
	    $('input#DOB_dd').css("border-color", "#AAA");
	    $('span#statusMessage').text('');
	    getBirthday('personAgeTxt','DOB_yyyy');
	} else {
	    obj.css("border-color", "#AAA");
	    $('span#statusMessage').hide();
	}
}

function autoTab(thisElem, maxLength, nextElem){
	if(thisElem.value.length == maxLength){
		thisElem.blur();
		$('#' + nextElem).focus();
	}
}
